/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.acrivity;

import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.widget.Toast;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.events.ItemSelectedEvent;
import com.bankscene.screenfit.fragment.MenusListFragment;
import com.bankscene.screenfit.util.ActivityUtils;
import com.bankscene.screenfit.vus.MainVu;
import com.bankscene.screenfit.vus.VuCallback;

import org.greenrobot.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author jiiiiiin
 */
public class MainActivity extends BasePresenterActivity<MainVu> {

    VuCallback<Integer> navigationItemSelectedCallback = new VuCallback<Integer>() {
        @Override
        public void execute(Integer menuId) {
            switch (menuId) {
                case R.id.home_navigation_menu_item:
                case R.id.list_navigation_menu_item:
                case R.id.idauthentication_navigation_menu_item:
                case R.id.fingerprint_navigation_menu_item:
                case R.id.touchiccard_navigation_menu_item:
                case R.id.nonetouchiccard_navigation_menu_item:
                case R.id.mscard_navigation_menu_item:
                case R.id.photocokkect_navigation_menu_item:
                case R.id.keyboard_navigation_menu_item:
                case R.id.pwddownload_navigation_menu_item:
                case R.id.writingpad_navigation_menu_item:
                case R.id.systeminfo_navigation_menu_item:
                case R.id.logout_navigation_menu_item:
                    Toast.makeText(MainActivity.this, R.string.dev, Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onBindVu() {
        // Set up the toolbar.
        setSupportActionBar(vu.getActionBar());
        ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);

        vu.setNavigationItemSelectedCallback(navigationItemSelectedCallback);

        ActivityUtils.addFragmentToActivity(fm, MenusListFragment.newInstance(), R.id.contentFrame);

        fm.beginTransaction()
                .replace(vu.getContainerId(), MenusListFragment.newInstance())
                .commit();

        Logger log = LoggerFactory.getLogger(MainActivity.class);
        log.info("onBindVu log");
    }

    @Override
    protected void afterResume() {
        bus.register(this);
    }

    @Override
    protected void beforePause() {
        //解注册
        bus.unregister(this);
    }

    @Override
    public boolean handleBackPressed() {
        bus.removeAllStickyEvents();
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // Open the navigation drawer when the home icon is selected from the toolbar.
                vu.getDrawerLayout().openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected Class<MainVu> getVuClass() {
        return MainVu.class;
    }

    @Subscribe(sticky = true)
    public void onEvent(ItemSelectedEvent event) {
        Toast.makeText(MainActivity.this, R.string.dev, Toast.LENGTH_LONG).show();
    }
}
